from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth import get_user_model
from .models import Chef, DjChef

User = get_user_model()

@receiver(post_save, sender=User)
def create_chef_profile(sender, instance, created, **kwargs):
    if created and instance.is_chef:  # Assuming you have an is_chef field on your User model
        Chef.objects.create(user=instance)

@receiver(post_save, sender=DjChef)
def sync_chef_status(sender, instance, **kwargs):
    """当DjChef状态变更时，自动同步到Chef模型"""
    try:
        chef = Chef.objects.get(djchef=instance)
        if chef.status != instance.user_status:
            chef.status = instance.user_status
            chef.save()
    except Chef.DoesNotExist:
        # 如果没有对应的Chef记录，可以选择创建或忽略
        pass